﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SystemicFunction.Public.CharacterService
{
    public class SearchHelper
    {
        #region 关键字拆分


        /// <summary>
        /// 将字符按照下面的要求拆分
        /// 1.全字符      1
        /// 2.单个字符    Length
        /// 3.词组字符    Length -1
        /// </summary>
        /// <param name="keywordStr"></param>
        /// <returns>返回按照长度倒叙排序的集合</returns>
        public static IList<string> MakeKeys(string keywordStr)
        {
            long keywordLen = keywordStr.Replace(" ", "").Length;
            //Int64 keysLen = 1 + keywordLen + ( keywordLen - 1 );
            var keys = new string[2 * keywordLen];
            char[] signChars = keywordStr.Replace(" ","").ToCharArray();

            for (int i = 0; i < keys.Length; )
            {
                keys[i] = signChars[i / 2].ToString();
                int nextCharIndex = i / 2 + 1;
                if (nextCharIndex < signChars.Length)
                {
                    keys[i + 1] = keys[i] + signChars[nextCharIndex];
                }
                i += 2;
            }

            keys[keys.Length - 1] = keywordStr;
            return keys.OrderByDescending(m => m.Length).ToList();
        }

        #endregion
    }
}
